package cn.gao.listener;

import cn.gao.constant.FileTidyConstant;
import cn.gao.enums.FinalFloderEnum;
import cn.gao.enums.child.*;
import cn.gao.enums.exclude.ExcludeEnum;
import cn.gao.utils.FileUtils;
import lombok.extern.slf4j.Slf4j;

import java.io.File;
import java.util.Arrays;
import java.util.HashSet;
import java.util.List;

import static java.lang.Thread.sleep;

/**
 * Created with IntelliJ IDEA.
 *
 * @Author: GaoXu
 * @Date: 2022/05/20/14:12
 * @Description:
 */
@Slf4j
public class FileListener {

    private HashSet<String> fileNameSet = new HashSet<>();

    public FileListener() {
        this.init_floder();

        this.listen().start();
    }

    /**
     * 监听文件变化
     *
     * @return
     */
    public Thread listen() {
        String listenFloderName = FileTidyConstant.LISTENFLODERNAME;
        File file = new File(listenFloderName);
        File[] files = file.listFiles();
        Arrays.stream(files).forEach(f -> {
            if (!f.isDirectory()) {
                fileNameSet.add(f.getName());
                // 初始化扫描
                file_tidy(f.getName());
            }
        });

        FileTidyConstant.FLODERFILECOUNT = files.length;
        Thread t = new Thread(new Runnable() {
            @Override
            public void run() {
                System.out.println("Download文件夹监控中...");
                while (true) {
                    try {
                        sleep(800);
                        File file = new File(listenFloderName);
                        File[] files = file.listFiles();
                        int fileCount = files.length;
                        Arrays.stream(files).forEach(f -> {
                            if (!new File(FileTidyConstant.LISTENFLODERNAME + "\\" + f.getName()).isDirectory()) {
                                String newFile = f.getName();
                                // 排除下载中缓存文件
                                if (!ExcludeEnum.contains(FileUtils.getFileSuffName(f.getName()))) {
                                    //开始整理
                                    file_tidy(newFile);
                                }
                            }
                        });
                        FileTidyConstant.FLODERFILECOUNT = fileCount;
//                        System.out.println(FileTidyConstant.FLODERFILECOUNT);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }

            }
        });
        return t;
    }

    public void file_tidy(String fileName) {
        String fileSuffName = FileUtils.getFileSuffName(fileName);
        String targetPath = switchFloder(fileSuffName);
        FileUtils.fileCut(FileTidyConstant.LISTENFLODERNAME + "\\" + fileName, targetPath + "\\" + fileName);
    }

    public static void main(String[] args) {
        System.out.println(switchFloder("pdf"));
    }

    public static String switchFloder(String fileSuffName) {
        String preName = FileTidyConstant.LISTENFLODERNAME + "\\";
        int flag = -1;
        if (ExeTypeEnum.contains(fileSuffName)) {
            flag = 0;
        } else if (ZipTypeEnum.contains(fileSuffName)) {
            flag = 1;
        } else if (ImgTypeEnum.contains(fileSuffName)) {
            flag = 2;
        } else if (DocTypeEnum.contains(fileSuffName)) {
            flag = 3;
        } else if (PdfTypeEnum.contains(fileSuffName)) {
            flag = 4;
        } else if (ExcelTypeEnum.contains(fileSuffName)) {
            flag = 5;
        } else if (VideoTypeEnum.contains(fileSuffName)) {
            flag = 7;
        }else if (JavaTypeEnum.contains(fileSuffName)) {
            flag = 8;
        } else {
            flag = 6;
        }
        return preName + (FinalFloderEnum.getPathNameByCode(flag).getPathName());
    }

    /**
     * 初始化文件夹
     */
    public void init_floder() {
        log.info("FileListener init_floder [初始化文件夹]");
        List<String> allFloder = FinalFloderEnum.getAllFloder();

        allFloder.stream().forEach(f -> {
            String floderName = FileTidyConstant.LISTENFLODERNAME + "\\" + f;
            File file = new File(floderName);
            if (!file.exists()) {
                log.info("FileListener init_floder [文件夹不存在，并创建] floder:{}", floderName);
                file.mkdirs();
            }
        });
        log.info("FileListener init_floder [初始化文件夹完成]");
    }
}
